package org.web.standardplatform.client.helper;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ResourceUtils;
import org.web.standardplatform.client.domain.buildproject.ProjectDefinitionDO;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.web.standardplatform.client.domain.buildproject.ProjectDefinitionConstant.HALF_WAY_LINE;

public class BuilderHelper {

    public static String getProjectTemplatePath(String baseTemplatePath) throws FileNotFoundException {
        if (StringUtils.isEmpty(baseTemplatePath)) {
            return ResourceUtils.getFile("").getAbsolutePath() + File.separator + "standard-platform-client" + File.separator + "target" + File.separator + "classes" + File.separator + "template"+ File.separator + "project";
        }
        return baseTemplatePath;
    }

    public static File getProjectOutputDirectory(ProjectDefinitionDO projectDefinitionDO) {
        File projectDirectory = new File(projectDefinitionDO.getOutputPath() + File.separator + projectDefinitionDO.getArtifactId());
        return projectDirectory;
    }

    public static Template getTemplate(String baseTemplatePath, String templateFileName) throws IOException {
        //1.创建配置类
        Configuration configuration = new Configuration(Configuration.getVersion());
        //2.设置模板所在的目录
        configuration.setDirectoryForTemplateLoading(new File(baseTemplatePath + File.separator));
        //3.设置字符集
        configuration.setDefaultEncoding("UTF-8");
        //4.加载模板
        Template template = configuration.getTemplate(templateFileName);
        return template;
    }

    public static void process(Template template, Writer out, ProjectDefinitionDO projectDefinitionDO) throws IOException, TemplateException {
        Map map = new HashMap();
        map.put("projectDefinitionDO", projectDefinitionDO);
        map.put("stringUtils", new StringUtils());
        template.process(map, out);
    }

    public static String transferString2Path(String groupId) {
        return groupId.replace(".", File.separator);
    }

    public static String transferLine2Humps(String name, String split, boolean isFirstPartInitUpper) {
        List<String> list = new ArrayList<>();
        String[] array = name.split(split);
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < array.length; i++) {
            if (i == 0) {
                if (isFirstPartInitUpper) {
                    result.append(initUpper(array[i]));
                } else {
                    result.append(initLower(array[i]));
                }
            } else {
                result.append(initUpper(array[i]));
            }

        }
        return result.toString();
    }

    public static String initUpper(String str) {
        char[] ch = str.toCharArray();
        if (ch[0] >= 'a' && ch[0] <= 'z') {
            ch[0] = (char) (ch[0] - 32);
        }
        return new String(ch);
    }

    public static String initLower(String str) {
        char[] ch = str.toCharArray();
        if (ch[0] >= 'A' && ch[0] <= 'Z') {
            ch[0] = (char) (ch[0] + 32);
        }
        return new String(ch);
    }

    public static String javaProjectBaseDirectory(String prefix, ProjectDefinitionDO projectDefinitionDO) {
        String path = transferString2Path(prefix + "." + projectDefinitionDO.getGroupId() + "." + transferLine2Humps(projectDefinitionDO.getArtifactId(), HALF_WAY_LINE, false));
        return path;
    }

}
